<template>
  <div class="drawer">
    <el-drawer title="管理用户" :visible.sync="drawer" direction="ltr" :before-close="handleClose">
      <div class="editForm">
        <el-form ref="editForm" :model="editForm" :rules="editRules">
          <el-form-item label="修改状态：" prop="status">
            <el-select v-model="editForm.status" placeholder="请选择">
              <el-option label="正常" value="0"></el-option>
              <el-option label="禁用" value="1"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button @click="drawer = false" size="mini">取 消</el-button>
            <el-button type="primary" @click="handleEdit" size="mini">修 改</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="reg">
        <el-button type="danger" @click="resetPwd">重置密码</el-button>
        <p>
          <i>*重置密码为‘123456’，请谨慎操作！</i>
        </p>
      </div>
    </el-drawer>

  </div>
</template>

<script>
import { updateAdminService, resetPwdService } from '@/api/admin'
export default {
  data () {
    return {
      id: '',
      role: '',
      drawer: false,
      editForm: {
        status: ''
      },
      editRules: {
        status: [
          { required: true, message: '请选择状态', trigger: 'blur' }
        ]
      },
      slectId: '',
      itemData: {}
    }
  },
  methods: {
    open (id, role) {
      this.id = id
      this.role = role
      this.drawer = true
    },
    handleClose (done) {
      this.id = ''
      this.role = ''
      done()
    },
    async resetPwd () {
      await resetPwdService(this.id, this.role).then(res => {
        if (res.status === 0) {
          this.$message({
            message: '重置密码成功',
            type: 'success'
          })
          this.drawer = false
        } else {
          this.$message({
            message: '重置密码失败',
            type: 'error'
          })
        }
      })
    },
    async handleEdit () {
      await updateAdminService(this.id, this.editForm.status, this.role).then(res => {
        if (res.status === 0) {
          this.$message({
            message: '设置成功',
            type: 'success'
          })
          // 调用父组件刷新数据
          this.$parent.getAdminInfo()
          this.editForm.status = ''
          this.drawer = false
        } else {
          this.$message({
            message: '设置失败',
            type: 'error'
          })
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.drawer{
  .editForm{
    margin: 0 30px;
  }
  .reg{
    width: 100%;
    margin-top: 30px;
    padding: 20px;
    text-align: center;
    border-top: 1px solid #ccc;
    /deep/ .el-button{
      width: 100%;
    }
    i{
      color: #999;
      font-size: 12px;
    }
  }
}
</style>
